//
//  CategorySubViewController.swift
//  EnglishDictionary
//
//  Created by QFX on 2024/2/23.
//

import UIKit

class CategorySubViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
    
    @IBOutlet weak var collectionWidthConstraint: NSLayoutConstraint!
    var categoryTitle:String?

    @IBOutlet weak var listCollectionView: UICollectionView!
    var folds:[Fold]?
    
    private lazy var listCollectionFlowLayout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        let width = 300
        let height = 300
        layout.itemSize = CGSize(width: width, height: height)
        layout.minimumLineSpacing = 10.0
        layout.minimumInteritemSpacing = 10.0
//        layout.sectionInset = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)
        layout.scrollDirection = .horizontal
        layout.sectionHeadersPinToVisibleBounds = true
        return layout
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = .white
        let scrollEdgeAppearance = UINavigationBarAppearance()
        scrollEdgeAppearance.configureWithOpaqueBackground()
        self.navigationController?.navigationBar.scrollEdgeAppearance = scrollEdgeAppearance
        self.navigationController?.navigationBar.standardAppearance = scrollEdgeAppearance
        self.navigationController?.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor = .black
        self.navigationItem.title = self.categoryTitle
        
        self.listCollectionView?.dataSource = self
        self.listCollectionView?.delegate = self
        self.listCollectionView?.collectionViewLayout = self.listCollectionFlowLayout
        self.listCollectionView?.register(UINib(nibName: "HomeCollectionCell", bundle: nil), forCellWithReuseIdentifier: "HomeCollectionCell")
        
        if let title = self.categoryTitle{
            self.folds = Fold.getFolds(parentFold: title)
        }
        self.collectionWidthConstraint.constant = CGFloat(300 * (self.folds?.count ?? 0))
    }

    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.folds?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let row = indexPath.row
        let fold = self.folds?[row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as? HomeCollectionCell
        cell?.titleLabel.text = fold?.fold
        let image = UIImage(named: "fold")
        cell?.foldImageView.image = image?.withRenderingMode(.alwaysTemplate)
        cell?.foldImageView.tintColor = UIColor.RGBColorHex(0x5DAF78)
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let row = indexPath.row
        let fold = self.folds?[row]
        let st = UIStoryboard.init(name: "Main", bundle: nil)
        let vc = st.instantiateViewController(withIdentifier: "ListViewController") as! ListViewController
        vc.navTitle = fold?.fold
        vc.categoryTitle = self.categoryTitle
        self.navigationController?.pushViewController(vc, animated: true)
    }
 
}
